package com.example.consumer.receiver;

import com.example.consumer.config.RabbitConfig;
import com.rabbitmq.client.Channel; //cline的channel
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaderMapper;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message; //注意是messageing（MQ）里的message对象
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
public class MsgReceiver {
//两个消费者监听同一个队列
    @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void handleMsg(Message message, Channel channel){
         //content:Object
        System.out.println(message.getPayload());

        //ack  告诉MQ消费成功
        try {
            channel.basicAck((Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG),false);//回送”当前消息头里的确认标记“，是否批量处理（单个消息fasle）
        } catch (IOException e) {
            System.out.println(e);
        }

    }

    @RabbitListener(queues = RabbitConfig.QUEUE_NAME,concurrency = "20") //thread
    public void handleMsg2(Message message, Channel channel) throws IOException{
        System.out.println(message.getPayload());
        //拒绝消费
        channel.basicReject((Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG),true); //第二个是requeue参数，被拒绝消息回送到队列

    }

}
